#include<stdio.h>
#include<conio.h>
#include<graphics.h>

int main()
{
	float width, height;
	width = 600;
	height = 400;
	initgraph(width, height);

	float ball_x, ball_y,gravity,ball_vy, radius;
	float  rect_left_x, rect_width, rect_height, rect_left_vx;
	radius = 20;
	ball_x = width / 4;
	ball_y =  radius;
	ball_vy = 0;
	gravity = 0.3;
	rect_left_vx = -3;
	rect_width = 20;
	rect_height = 100;
	rect_left_x = width / 4*3;
	int score = 0;
	while (1)
	{
		if (_kbhit())
		{
			char input = _getch();
			if (input == ' ')
			{
				ball_vy = -10;
			}
			

		}
		ball_vy = ball_vy + gravity;
		ball_y = ball_y + ball_vy;
		
		if (ball_y >= height - radius)
		{
			ball_vy = 0;
			ball_y = height - radius;
		}
		rect_left_x = rect_left_x + rect_left_vx;
		if (rect_left_x <= 0)
		{
			rect_left_x = width;
			score = score + 1;
			rect_left_vx = rand() / float(RAND_MAX) * 4 - 7;
			rect_height = rand() % int(height / 4) + height / 4;
		}
		if ((rect_left_x <= ball_x + radius) && (rect_left_x + rect_width >= ball_x - radius) && (ball_y + radius >= height - rect_height))
		{
			Sleep(100);
			score = 0;
		}

		cleardevice();
		fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width, height);
		fillcircle(ball_x, ball_y, radius);
        TCHAR s[20];
		swprintf_s(s, _T("%d"), score);
		settextstyle(40, 0, _T(""));
		outtextxy(50, 30, s);
		Sleep(10);
		

	}

	
	_getch();
	closegraph();
	
	return 0;
}

